package cscie160.project;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class AccountImplementation extends UnicastRemoteObject implements Account {
    /**  The amount of money held in the account */
    private float   balance;

    public AccountImplementation() throws RemoteException
    {
        balance = 0;
    }

    /**
     * Deposit an amount of money into our account
     * @param amount  The amount to be deposited
     * @throws java.rmi.RemoteException Thrown if depositing a negative amount of money
     */
    public void deposit(float amount) throws RemoteException
    {
        // Don't allow deposits of negative money
        if (amount < 0)
        {
            throw new RemoteException("Depositing negative amount");
        }
        balance += amount;
        System.out.println("Account deposited " + amount + " to balance " + balance);
    }

    /**
     * Withdraw an amount of money from our account
     * @param amount  The amount to be withdrawn
     * @throws RemoteException Thrown if withdrawing a negative amount of money or more money than is in the account
     */
    public void withdraw(float amount) throws RemoteException
    {
        // Don't allow withdrawals of negative money
        if (amount < 0)
        {
            throw new RemoteException("Withdrawing negative amount");
        }
        // Don't allow a withdrawal of more money than is in account
        if (amount > balance)
        {
            throw new RemoteException("Withdrawing more money than is available in balance");
        }
        balance -= amount;
        System.out.println("Account withdrew " + amount + " to balance " + balance);
    }

    /**
     * Returns balance of account
     * @return float The account balance
     */
    public float getBalance() throws RemoteException
    {
        return balance;
    }

}
